//app.js
import {
    User
} from "model/User.js";
const objUser = new User();
App({
    onLaunch: function() {
        wx.checkSession({　　
                success: function(res) {
                    console.log("处于登录态");
                },
                fail: function(res) {
                    // 登录
                    wx.login({
                        success: res => {
                            if (res.code) {
                                objUser.login({
                                        code: res.code
                                    })
                                    .then((res) => {
                                        wx.setStorageSync("token", res.data.Token);
                                        wx.setStorageSync("uId", res.data.UserId);
                                    })
                                    .catch((err) => {
                                        wx.showToast({
                                            title: err.message,
                                            icon: "none"
                                        })
                                    });
                            } else {
                                wx.showToast({
                                    title: '登录失败',
                                    icon: "none"
                                })
                            }
                        }
                    })　　　　
                }　　
            })
            // 获取用户信息
        wx.getSetting({
            success: res => {
                if (res.authSetting['scope.userInfo']) {
                    wx.getUserInfo({
                        success: res => {
                            this.globalData.userInfo = res.userInfo
                            if (this.userInfoReadyCallback) {
                                this.userInfoReadyCallback(res)
                            }
                        }
                    })
                }
            }
        })

        const sId = wx.getStorageSync("schoolId");
        if (!sId) {
            objUser.getUserInfo()
                .then(res => {
                    if (res.data.Avatar && res.data.Nickname){
                      wx.setStorageSync("avatar", res.data.Avatar);
                      wx.setStorageSync("nickName", res.data.Nickname);
                      wx.setStorageSync("gender", res.data.Gender);
                      wx.setStorageSync("isauth", true);
                    }
                    let schoolId = res.data.School.SchoolId;
                    if (schoolId) {
                        wx.setStorageSync("schoolId", schoolId);
                    } else {
                        wx.reLaunch({
                            url: '/pages/schools/index'
                        })
                    }
                })
                .catch(err => {
                    wx.showToast({
                        title: err.msg,
                        icon: "none"
                    })
                });
        }
    },
    globalData: {
        userInfo: null
    }
})